﻿using Mododor.Ext;
using Mododor.JsonModels;
using Mododor.Models;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Mododor.ViewInterfaces
{
    public class ViewInterface<T> : BindableBase
    {
        public IDialogService _dialogService;
        public HttpHelper _httpHelper;
        public ViewInterface(IDialogService dialogService, HttpHelper httpHelper)
        {
            _dialogService = dialogService;
            this._httpHelper = httpHelper;
            CompanyList = new ObservableCollection<CompanyInfoModel>();
            DataStatsValueList = new ObservableCollection<T>();
            ContractTypeModelList = new ObservableCollection<ContractTypeModel>();
            ClientList = new ObservableCollection<ClientInfoModel>();
        }

        public ObservableCollection<T> DataStatsValueList { get; set; }
        public ObservableCollection<CompanyInfoModel> CompanyList { get; set; }
        public ObservableCollection<ContractTypeModel> ContractTypeModelList { get; set; }
        public ObservableCollection<ClientInfoModel> ClientList { get; set; }
        //public DelegateCommand<T> SelectionChangedDataGrid { get; set; }
        public DelegateCommand MarkingDeleteCommand { get; set; }
        public DelegateCommand SaveDataCommand { get; set; }
        public DelegateCommand ClearDataCommand { get; set; }

        protected async virtual Task GetCompanyList()
        {
            CompanyList.Clear();
            string res = await _httpHelper.GetJson(ExtUrl.CompanySearchAllUrl);
            CompanyInfoJsonSearchModel.Rootobject root = JsonConvert.DeserializeObject<CompanyInfoJsonSearchModel.Rootobject>(res);
            foreach (CompanyInfoJsonSearchModel.Res msg in root.res)
            {
                CompanyList.Add(new CompanyInfoModel()
                {
                    CompanyAdd = msg.companyAdd,
                    CompanyCity = msg.companyCity,
                    CompanyNameCh = msg.companyNameCh,
                    CompanyNameEn = msg.companyNameEn,
                    CompanyTel = msg.companyTel,
                    CompanyZip = msg.companyZip,
                    Id = msg.id,
                    IdCompany = msg.idCompany,
                });
            }
        }

        protected async virtual Task GetContractTypeList()
        {
            ContractTypeModelList.Clear();
            string json = await _httpHelper.GetJson(ExtUrl.ContractTypeSearchAllUrl);
            ContractTypeInfoJsonSearchModel.Rootobject root = JsonConvert.DeserializeObject<ContractTypeInfoJsonSearchModel.Rootobject>(json);
            foreach (var item in root.res)
            {
                ContractTypeModelList.Add(new ContractTypeModel()
                {
                    ContractTypeDescription = item.contractTypeDescription,
                    ContractTypeName = item.contractTypeName,
                     IdContractType = item.idContractType,
                });
            }
        }

        protected async virtual Task GetClientList(long idCompany)
        {
            ClientList.Clear();
            string json = await _httpHelper.GetJson($"{ExtUrl.ClientSearchAllUrl}?idCompany={idCompany}");
            ClientInfoJsonSearchModel.Rootobject root = JsonConvert.DeserializeObject<ClientInfoJsonSearchModel.Rootobject>(json);
            foreach (var item in root.res)
            {
                ClientList.Add(new ClientInfoModel()
                {
                    ClientEmail = item.clientEmail,
                    ClientJobTitle = item.clientJobTitle,
                    ClientMobile = item.clientMobile,
                    ClientNameCh = item.clientNameCh,
                    ClientNameEn = item.clientNameEn,
                    idClient = item.idClient,
                    Client_IdCompany = item.idClient,
                    CompanyNameCh = item.companyNameCh,
                });
            }
        }

        protected async virtual Task SnackBarWaiting()
        {
            SnackBarActive = true;
            await Task.Delay(2000);
            SnackBarActive = false;
        }

        private bool isOpen;

        public bool IsOpen
        {
            get { return isOpen; }
            set { isOpen = value; RaisePropertyChanged(); }
        }
        private bool snackBarActive;

        public bool SnackBarActive
        {
            get { return snackBarActive; }
            set { snackBarActive = value; RaisePropertyChanged(); }
        }
        private string snackBarMessage;

        public string SnackBarMessage
        {
            get { return snackBarMessage; }
            set { snackBarMessage = value; RaisePropertyChanged(); }
        }

    }
}
